'use strict';

module.exports = app => {
  const { model } = app;
  const { UUID, ENUM } = app.Sequelize;

  const Permission = model.define(
    'User',
    {
      id: {
        type: UUID,
        primaryKey: true,
      },
      type: {
        type: ENUM,
        values: [ '0', '1' ],
      },
      action: {
        type: ENUM,
        values: [ '0', '1', '2', '3' ],
      },
    },
    {
      tableName: 'sys_permission',
    }
  );

  Permission.associate = function() {
    const { Sys: { Role, Permission, RolePermission, Resource, PermissionResource, Menu, PermissionMenu } } = model;
    Permission.belongsToMany(Role, { as: 'roles', foreignKey: 'permission_id', through: RolePermission });
    Permission.belongsToMany(Resource, { as: 'resource', foreignKey: 'permission_id', through: PermissionResource });
    Permission.belongsToMany(Menu, { as: 'menu', foreignKey: 'permission_id', through: PermissionMenu });
  };

  return Permission;
};
